//
//  InputTempletThreeTableViewCell.m
//  CedarFinancePH
//
//  Created by menchao on 2018/4/2.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "InputTempletThreeTableViewCell.h"

@interface InputTempletThreeTableViewCell ()

@property (nonatomic,strong) UILabel *titleLbl;
@property (nonatomic,strong) UIView *leftView;


@end



@implementation InputTempletThreeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self!=nil){
        self.inputMaxLength = 0;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLbl];
        [self.contentView addSubview:self.inputTextField];
        [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
            make.centerY.mas_equalTo(self.contentView);
        }];
        [self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(120);
            make.right.mas_equalTo(self.contentView).mas_offset(-kMarginOffsetX);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)setLeftView:(UIView *)leftView size:(CGSize)size{
    if(_leftView){
        [_leftView removeFromSuperview];
    }
    _leftView = leftView;
    if(leftView){
        [self.contentView addSubview:_leftView];
        [_leftView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLbl.mas_right).mas_offset(7);
            make.centerY.mas_equalTo(self.contentView);
            make.size.mas_offset(size);
        }];
    }
}


- (void)setPlaceholder:(NSString *)placeholder{
    self.inputTextField.placeholder = placeholder;
    [UIConfigure configureUITextField:self.inputTextField fontSize:14 color:kGrayTextColor];
}

- (void)setCellTitle:(NSString *)cellTitle{
    self.titleLbl.text = cellTitle;
}

- (void)textFieldDidChange:(UITextField *)textField{
    if (self.inputMaxLength>0) {
        if (textField.text.length > self.inputMaxLength) {
            textField.text = [textField.text substringToIndex:self.inputMaxLength];
        }
    }
    if (self.textChangeBlock) {
        self.textChangeBlock(self.tag,textField.text);
    }
}


MACRO_UILabel_LazyloadWithPFCGFont(titleLbl,@"",kGrayColor, 14)

- (UITextField *)inputTextField{
    if (!_inputTextField) {
        _inputTextField = [[UITextField alloc]init];
        [_inputTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_inputTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        _inputTextField.font = PingFangZhongChangGuiForFont(15);
      }
    return _inputTextField;
}

+ (CGFloat)heightForCell{
    return 50.0;
}

@end



@interface InputBankCardTableViewCell ()<UITextFieldDelegate>

@property (nonatomic,strong) UILabel *titleLbl;
@property (nonatomic,strong) UIView *leftView;


@end



@implementation InputBankCardTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self!=nil){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
//        [self.contentView addSubview:self.titleLbl];
        [self.contentView addSubview:self.inputTextField];
//        [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
//            make.centerY.mas_equalTo(self.contentView);
//        }];
        [self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
            make.right.mas_equalTo(self.contentView).mas_offset(-kMarginOffsetX);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)setLeftView:(UIView *)leftView size:(CGSize)size{
    if(_leftView){
        [_leftView removeFromSuperview];
    }
    _leftView = leftView;
    if(leftView){
        [self.contentView addSubview:_leftView];
        [_leftView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLbl.mas_right).mas_offset(7);
            make.centerY.mas_equalTo(self.contentView);
            make.size.mas_offset(size);
        }];
    }
}


- (void)setPlaceholder:(NSString *)placeholder{
    self.inputTextField.placeholder = placeholder;
    [UIConfigure configureUITextField:self.inputTextField fontSize:14 color:kGrayTextColor];
}

- (void)setCellTitle:(NSString *)cellTitle{
    self.titleLbl.text = cellTitle;
}

- (void)textFieldDidChange:(UITextField *)textField{
   
    if (self.textChangeBlock) {
        self.textChangeBlock(self.tag,self.inputTextField.text);
    }
}


//- (NSString *)normalBankCard:(NSString *)sourceStr{
//    NSString *text = [sourceStr stringByReplacingOccurrencesOfString:@" " withString:@""];
//    return text;
//}
//
//
//
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//
//    NSString *text = [textField text];
//
//    CLog(@"text: %@,location:%d,length:%lu, string: %@", text, range.location,(unsigned long)range.length,  string);
//    NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
//    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
//    if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
//        return NO;
//    }
//    text = [text stringByReplacingCharactersInRange:range withString:string];
//    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
//    NSString *newString = @"";
//    while (text.length > 0){
//        NSString *subString = [text substringToIndex:MIN(text.length, 4)];
//        newString = [newString stringByAppendingString:subString];
//        if (subString.length == 4) {
//            newString = [newString stringByAppendingString:@" "];
//        }
//        text = [text substringFromIndex:MIN(text.length, 4)];
//    }
//    newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
//    [textField setText:newString];
//
//    if (self.textChangeBlock) {
//        NSString *sourceBankCard = [self normalBankCard:self.inputTextField.text];
//         CLog(@"sourceBankCard: %@",sourceBankCard);
//        self.textChangeBlock(textField.tag,sourceBankCard);
//    }
//    return NO;
//}


MACRO_UILabel_LazyloadWithPFCGFont(titleLbl,@"",kGrayColor, 14)

- (UITextField *)inputTextField{
    if (!_inputTextField) {
        _inputTextField = [[UITextField alloc]init];
        [_inputTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_inputTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        _inputTextField.font = PingFangZhongChangGuiForFont(15);
        _inputTextField.delegate = self;
        _inputTextField.userInteractionEnabled = YES;
        _inputTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _inputTextField;
}

+ (CGFloat)heightForCell{
    return 50.0;
}

@end
